package com.firstresponder.activity;

import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MyExpandableApparatusListAdapter extends BaseExpandableListAdapter {
	
	private String TAG="MyExpandableApparatusListAdapter";
	private String[] groups = { "     Locations", "     Responders", "     Apparatus" };
	private String[][] children = {
			{ "", " "},//Just setting two child spaces which will hold two layouts
					           //one for apparatus(apparatus is child # 3 though) TextFields and other for bottom buttons
			{ " ", " " },
			{ " ", " " },
			{ "Child4" }, { "Child5" } };
	private static FirstResponder MAIN_ACTIVITY = null;
	private int grayishColor = 0xFFDDE1DD;
	// buttons that have to be dynamically places inside the expandable list
	// view
	private Button apparatusSort,apparatusFilter,apparatusRefresh;

	public Object getChild(int groupPosition, int childPosition) {
		return children[groupPosition][childPosition];

	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public int getChildrenCount(int groupPosition) {
		int i = 0;
		try {
			i = children[groupPosition].length;

		} catch (Exception e) {
		}

		return i;
	}

	public TextView getGenericTextView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, 64);

		TextView textView = new TextView(MAIN_ACTIVITY);
		textView.setLayoutParams(lp);
		// Center the text vertically
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		textView.setTextColor(grayishColor);
		// Set the text starting position
		textView.setPadding(36, 0, 0, 0);
		return textView;
	}

	// test to add dynamic thigns to the child
	public LinearLayout getGenericLinearLayoutView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		LinearLayout linearLayout = new LinearLayout(MAIN_ACTIVITY);
		linearLayout.setLayoutParams(lp);

		// Set the text starting position
		//linearLayout.setPadding(36, 0, 0, 0);
		return linearLayout;
	}
	public LinearLayout getGenericLinearLayoutView1() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

		LinearLayout linearLayout = new LinearLayout(MAIN_ACTIVITY);
		linearLayout.setLayoutParams(lp);

		// Set the text starting position
		//linearLayout.setPadding(36, 0, 0, 0);
		return linearLayout;
	}
	public ScrollView getGenericScrollView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

		ScrollView scrollview = new ScrollView(MAIN_ACTIVITY);
		scrollview.setLayoutParams(lp);

		// Set the text starting position
		//scrollview.setPadding(36, 0, 0, 0);
		return scrollview;
	}
	public HorizontalScrollView getGenericHorizontalScrollView() {
		// Layout parameters for the ExpandableListView
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

		HorizontalScrollView horizontalScrollview = new HorizontalScrollView(MAIN_ACTIVITY);
		horizontalScrollview.setLayoutParams(lp);

		// Set the text starting position
		//scrollview.setPadding(36, 0, 0, 0);
		return horizontalScrollview;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		Log.i(TAG, "getCHildView() Called!");
		if (groupPosition == 0) {
			
				TextView textView = getGenericTextView();
				textView.setText(getChild(groupPosition, childPosition)
						.toString());
				return textView;
			

			// MyExpandableListAdapter listener			
		}
		if (groupPosition == 1) {
			TextView textView = getGenericTextView();
			textView.setText(getChild(groupPosition, childPosition)
					.toString());
			return textView;
			
		} else {//groupPosition==2 Apparatus
			if (childPosition == 0) {
				//NOTE:this code will be significantly reduced when items are added dynamically from an array
				LinearLayout itemsLayout=new LinearLayout(MAIN_ACTIVITY);
				itemsLayout.setOrientation(0);
				AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
						ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				itemsLayout.setLayoutParams(lp);
				
				LinearLayout deptLayout=getGenericLinearLayoutView();
				deptLayout.setOrientation(1);
				TextView dep0=getGenericTextView();
				dep0.setText("Dept");
				TextView dep1=getGenericTextView();
				dep1.setText(" 25");
				TextView dep2=getGenericTextView();
				dep2.setText(" 25");
				TextView dep3=getGenericTextView();
				dep3.setText(" 25");
				TextView dep4=getGenericTextView();
				dep4.setText(" 42");
				TextView dep5=getGenericTextView();
				dep5.setText(" 42");
				deptLayout.addView(dep0);
				deptLayout.addView(dep1);
				deptLayout.addView(dep2);
				deptLayout.addView(dep3);
				deptLayout.addView(dep4);
				deptLayout.addView(dep5);
				
				LinearLayout idLayout=getGenericLinearLayoutView();
				idLayout.setOrientation(1);
				TextView id0=getGenericTextView();
				id0.setText(" ID");
				TextView id1=getGenericTextView();
				id1.setText(" E1");
				TextView id2=getGenericTextView();
				id2.setText(" E2");
				TextView id3=getGenericTextView();
				id3.setText(" R1");
				TextView id4=getGenericTextView();
				id4.setText(" TN6");
				TextView id5=getGenericTextView();
				id5.setText(" IC1");
				idLayout.addView(id0);
				idLayout.addView(id1);
				idLayout.addView(id2);
				idLayout.addView(id3);
				idLayout.addView(id4);
				idLayout.addView(id5);
				
				LinearLayout typeLayout=getGenericLinearLayoutView();
				typeLayout.setOrientation(1);
				TextView type0=getGenericTextView();
				type0.setText(" Type");
				TextView type1=getGenericTextView();
				type1.setText(" Pumper");
				TextView type2=getGenericTextView();
				type2.setText(" Pumper");
				TextView type3=getGenericTextView();
				type3.setText(" Rescue");
				TextView type4=getGenericTextView();
				type4.setText(" Tender");
				TextView type5=getGenericTextView();
				type5.setText(" Cmd");
				typeLayout.addView(type0);
				typeLayout.addView(type1);
				typeLayout.addView(type2);
				typeLayout.addView(type3);
				typeLayout.addView(type4);
				typeLayout.addView(type5);
				
				LinearLayout statusLayout=getGenericLinearLayoutView();
				statusLayout.setOrientation(1);
				TextView status0=getGenericTextView();
				status0.setText(" Status");
				TextView status1=getGenericTextView();
				status1.setText(" Awaiting Crew");
				TextView status2=getGenericTextView();
				status2.setText(" Arriving On Scene");
				TextView status3=getGenericTextView();
				status3.setText(" Arriving On Scene");
				TextView status4=getGenericTextView();
				status4.setText(" En Route");
				TextView status5=getGenericTextView();
				status5.setText(" In Quarters");
				statusLayout.addView(status0);
				statusLayout.addView(status1);
				statusLayout.addView(status2);
				statusLayout.addView(status3);
				statusLayout.addView(status4);
				statusLayout.addView(status5);
				
				LinearLayout etaLayout=getGenericLinearLayoutView();
				etaLayout.setOrientation(1);
				TextView eta0=getGenericTextView();
				eta0.setText(" ETA");
				TextView eta1=getGenericTextView();
				eta1.setText(" 2:14");
				TextView eta2=getGenericTextView();
				eta2.setText(" 2:30");
				TextView eta3=getGenericTextView();
				eta3.setText(" 6:38");
				TextView eta4=getGenericTextView();
				eta4.setText(" 7:10");
				TextView eta5=getGenericTextView();
				eta5.setText(" NA");
				etaLayout.addView(eta0);
				etaLayout.addView(eta1);
				etaLayout.addView(eta2);
				etaLayout.addView(eta3);
				etaLayout.addView(eta4);
				etaLayout.addView(eta5);
				
				LinearLayout destLayout=getGenericLinearLayoutView();
				destLayout.setOrientation(1);
				TextView dest0=getGenericTextView();
				dest0.setText(" DEST");
				TextView dest1=getGenericTextView();
				dest1.setText(" Scene");
				TextView dest2=getGenericTextView();
				dest2.setText(" Scene");
				TextView dest3=getGenericTextView();
				dest3.setText(" Scene");
				TextView dest4=getGenericTextView();
				dest4.setText(" Draft Alpha");
				TextView dest5=getGenericTextView();
				dest5.setText(" NA");
				destLayout.addView(dest0);
				destLayout.addView(dest1);
				destLayout.addView(dest2);
				destLayout.addView(dest3);
				destLayout.addView(dest4);
				destLayout.addView(dest5);
				
				itemsLayout.addView(deptLayout);
				itemsLayout.addView(idLayout);
				itemsLayout.addView(typeLayout);
				itemsLayout.addView(statusLayout);
				itemsLayout.addView(etaLayout);
				itemsLayout.addView(destLayout);
				
				HorizontalScrollView HscrollItemLayout=getGenericHorizontalScrollView();
				HscrollItemLayout.addView(itemsLayout);
				
				ScrollView scrollItemLayout = getGenericScrollView();
				scrollItemLayout.addView(HscrollItemLayout);
				
				
				
				LinearLayout mainlinearlayout = getGenericLinearLayoutView1();
				mainlinearlayout.setOrientation(1);
				mainlinearlayout.addView(scrollItemLayout);
				
				
				return mainlinearlayout;
			}
			if (childPosition == 1) {
				apparatusSort = new Button(MAIN_ACTIVITY);
				apparatusFilter = new Button(MAIN_ACTIVITY);
				apparatusRefresh=new Button(MAIN_ACTIVITY);
				
				apparatusSort.setText(" Sort ");
				apparatusFilter.setText(" Filter ");
				apparatusRefresh.setText(" Refresh ");
				LinearLayout linearlayout = getGenericLinearLayoutView();
				linearlayout.setOrientation(0);
				linearlayout.addView(apparatusSort);
				linearlayout.addView(apparatusFilter);
				linearlayout.addView(apparatusRefresh);
				
				return linearlayout;
			}
			
		}

		return null;

	}

	public Object getGroup(int groupPosition) {
		return groups[groupPosition];
	}

	public int getGroupCount() {
		return groups.length;
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView textView = getGenericTextView();
		textView.setText(getGroup(groupPosition).toString());
		return textView;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	public static void setMainActivity(FirstResponder activity) {
		MAIN_ACTIVITY = activity;
	}

}
